package com.thinkfit.jy.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.common.lib.bean.VideoBean;
import com.common.lib.constant.Constants;
import com.common.lib.fragment.BaseFragment;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.thinkfit.jy.R;
import com.thinkfit.jy.activity.VideoCourseInfoActivity;
import com.thinkfit.jy.adapter.VideoRecordAdapter;
import com.thinkfit.jy.contract.TrainingCourseContract;
import com.thinkfit.jy.presenter.TrainingCoursePresenter;

import java.util.ArrayList;

/**
 * 用户训练课程记录
 */
public class TrainingCourseFragment extends BaseFragment<TrainingCourseContract.Presenter>
        implements TrainingCourseContract.View, OnRefreshLoadMoreListener {
    private UserProfileFragment mUserProfileFragment;
    private VideoRecordAdapter mAdapter;
    private int mPage;

    @NonNull
    @Override
    protected TrainingCourseContract.Presenter onCreatePresenter() {
        return new TrainingCoursePresenter(this);
    }

    @Override
    protected int getLayoutId() {
//        return DataManager.Companion.getInstance().getMainStyle() == 3 ?
//                R.layout.fragment_training_course_xm :
        return  R.layout.fragment_training_course;
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        getAdapter().onAttachedToRecyclerView(recyclerView);
        recyclerView.setAdapter(getAdapter());
        SmartRefreshLayout layout = view.findViewById(R.id.smartRefreshLayout);
        layout.setOnRefreshLoadMoreListener(this);
        layout.setEnableLoadMore(false);
        layout.autoRefresh();
    }

    private VideoRecordAdapter getAdapter() {
        if (mAdapter == null) {
            mAdapter = new VideoRecordAdapter(getActivity());
            mAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                    VideoBean bean = mAdapter.getItem(position);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(Constants.BUNDLE_EXTRA, bean);
                    bundle.putSerializable(Constants.BUNDLE_EXTRA_2, bean.getType());
                    openActivity(VideoCourseInfoActivity.class, bundle);
//                    openActivity(bean.getType() == 1 ? VideoCourseDetailActivity.class : LiveVideoActivity.class, bundle);
                }
            });
        }
        return mAdapter;
    }

    public TrainingCourseFragment setParentFragment(UserProfileFragment fragment) {
        mUserProfileFragment = fragment;
        return this;
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void getVideoRecordSuccess(int page, ArrayList<VideoBean> list) {
        if (getView() == null) {
            return;
        }
        mPage = page;
        if (page == 1) {
            getAdapter().setNewInstance(list);
        } else {
            getAdapter().addData(list);
        }
        finishLoad();
    }

    @Override
    public void getVideoRecordFailed() {
        if (getView() == null) {
            return;
        }
        finishLoad();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        getPresenter().getVideoRecord(mPage + 1);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        getPresenter().getVideoRecord(1);
    }

    private void finishLoad() {
        SmartRefreshLayout layout = getView().findViewById(R.id.smartRefreshLayout);
        layout.finishLoadMore();
        layout.finishRefresh();
        layout.setEnableLoadMore(getAdapter().getItemCount() != 0
                && getAdapter().getItemCount() % 9 == 0);
    }
}
